

#import "NetWorkManager.h"
#import "NetWorkDefine.h"

@implementation NetWorkManager

/** 单例 */
+(instancetype)sharedManager{
    
    static NetWorkManager *instance = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        
        instance = [[NetWorkManager alloc] init];
        
    });
    return instance;
}


/** 请求天气 */
- (void)loadWeatherWithCurrentCity:(NSString *)currentCity
                      successBlock:(void(^)(NSDictionary *cityDic))successBlock
                        errorBlock:(void(^)(NSString *error))errorBlock{
    
    
    if ([[NetWorkTool shareManager] isReachableByAFN]) {
        
        NSString *city = currentCity;
        NSString *url = @"http://api.map.baidu.com/telematics/v3/weather";
        
        NSDictionary *paramDic = @{@"location":city,
                                   @"output":@"json",
                                   @"ak":@"17IvzuqmKrK1cGwNL6VQebF9"
                                   };
        
        [[NetWorkTool shareManager] GET_RequestWithUrlString:url parmas:paramDic successBloock:^(id responseObject) {
            
            NSInteger errorCode = [responseObject[@"error"] integerValue];
            
            if (errorCode == 0) {
                
                NSDictionary *dic = responseObject[@"results"][0][@"weather_data"][0];
                
                successBlock(dic);
                
            }else{
                
                NSLog(@"错误");
                
                NSLog(@"没有此城市数据");
            }
            
        } failureBlock:^(NSError *error) {
            
            NSLog(kUnKnownFailureStatus);
            
        }];
        
    }else{
        
        NSLog(kNetWorkFailureStatus);
        
    }
}

#pragma
#pragma mark - 4.获取医生信息
- (void)loadDoctorsIfonWithParmas:(NSDictionary *)paramDic completeBlock:(void(^)(NSArray<QcDoctorsIfonModel *> *modelList,NSError *error))completeBlock errorBlock:(void(^)(NSString *error))errorBlock
{
    NSString *urlString = kMatchDoctors;
    
    if ([[NetWorkTool shareManager] isReachableByAFN]){
        
        [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(id responseObject) {
            
            NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            
            NSArray *stringArray = [str componentsSeparatedByString:@"<br />"];
            
            NSData *data = [stringArray[2] dataUsingEncoding:NSUTF8StringEncoding];
            
            
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
            
            NSArray *dictArray = dict[@"data"];
            
            NSArray<QcDoctorsIfonModel *>* modelList = [NSArray yy_modelArrayWithClass:[QcDoctorsIfonModel class] json:dictArray];
            
            completeBlock(modelList,nil);
            
            
        } failureBlock:^(NSError *error) {
            
            completeBlock(nil,error);
            
        }];
        
    }else{
        
        errorBlock(kNetWorkFailureStatus);
    }
    
}
#pragma mark -
#pragma mark - 获取关注医生列表

- (void)loadAttentionDoctorListWithUserID:(NSNumber *)usersId pageSize:(NSInteger)pageSize page:(NSInteger)page completeBlock:(void(^)(NSArray<QcDoctorsIfonModel *> *modelList,NSError *error))completeBlock errorBlock:(void(^)(NSString *error))errorBlock
{
    NSString *urlString = kDoctorListUrl;
    NSDictionary *parmas = @{@"user_id" :usersId,
                             @"page_size" :@(pageSize),
                             @"page" :@(page)};
    
    if ([[NetWorkTool shareManager] isReachableByAFN])
    {
        [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:parmas successBloock:^(id responseObject) {
            
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:NULL];
            
            NSArray *dataArray = dict[@"data"];
            
            NSArray<QcDoctorsIfonModel *> *array = [NSArray yy_modelArrayWithClass:[QcDoctorsIfonModel class] json:dataArray];
            
            completeBlock(array,nil);
            
        } failureBlock:^(NSError *error) {
            
            completeBlock(nil,error);
        }];
        
    }else
    {
        errorBlock(kNetWorkFailureStatus);
    }
}


/*
 //#疾病细分接口-------------------------------------------------
 // 肿瘤     ci1_id = 1
 // 心血管   ci1_id = 2
 // 神经科   ci1_id = 3
 // 血液科   ci1_id = 4
 // 骨科     ci1_id = 5
 // 公益活动  ci1_id = 6
 */
/**
 *  肿瘤疾病选择疾病细分列表接口
 */
-(void)loadDiseaseSubWithCi_id:(int)ci_id
                          page:(int)page
                  successBlock:(void(^)(NSArray *caseArray))successBlock
                    errorBlock:(void(^)(NSString *error))errorBlock{
    
    NSString *urlString = kSearchCI3Url;
    NSDictionary *paramDic = @{
                               @"ci1_id":@(ci_id),
                               @"page_size":@(page),
                               @"page":@1,
                               @"keyword":@""
                               };
    
    if ([[NetWorkTool shareManager] isReachableByAFN]) {
        
        [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(id responseObject) {
            
            NSArray *arr = responseObject[@"data"];
            
            NSMutableArray *tempArray = [NSMutableArray array];
            
            for (NSDictionary *dic in arr) {
                
                QcCaseChooseModel *caseChoose = [QcCaseChooseModel diseaseWithDic:dic];
                
                [tempArray addObject:caseChoose];
            }
            
            successBlock(tempArray);
            
        } failureBlock:^(NSError *error) {
            
            NSLog(@"error = %@",error);
        }];
        
    }else{
        
        errorBlock(kNetWorkFailureStatus);
    }
}


@end
